import Vue from 'vue'
import store from '@/store'
import router from '@/router'
import axios from 'axios'
import { getCookie } from './cookie'
import { Message } from 'element-ui'

const request = axios.create({
  // 仅在开发环境走代理
  baseURL: process.env.NODE_ENV === 'development' ? '' : 'http://yahansi.demo2.dx623.com'
})

request.interceptors.request.use(
  config => {
    // 登录后所有请求附带token
    if (store.state.isLogin ) {
      config.headers.Authorization = 'Bearer '+getCookie('token')
    }

    return config
  },
  err => {
    return Promise.reject(err)
  }
)

request.interceptors.response.use(
  res => {
    if (res.data.code ==1) {
      // 操作成功
      if (res.data.msg) Message.success(res.data.msg)
    } else if (res.data.msg) {
      // 失败
      Message(res.data.msg)
    }

    if (res.data.code==1) return res.data
    return Promise.reject(res.data)
  },
  err => {
    // token 失效
    if (err.response && err.response.status === 401) {
      Message.error('请重新登陆')
      store.commit('LOGIN_OUT')
      router.push('/login')
    } else {
      Message.error('网络错误，请重试')
    }

    return Promise.reject(err)
  }
)

Vue.prototype.$http = request

export default request
